<template>
  <div>
    <el-row :gutter="10" style="margin-bottom:60px">
      <el-col :span="6">
        <el-card style="color:#409EFF">
          <div><i class="el-icon-user-solid"/>用户数</div>
          <div style="padding:10px 0;text-align:center;font-weight:bold">{{ userCount }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card style="color:#F56C6C">
          <div><i class="el-icon-money"/>车辆数</div>
          <div style="padding:10px 0;text-align:center;font-weight:bold">{{ goodsCount }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card style="color:#67C23A">
          <div><i class="el-icon-bank-card"/>订单数</div>
          <div style="padding:10px 0;text-align:center;font-weight:bold">{{ orderCount }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card style="color:#409EFF">
          <div style="color:#409EFF"><i class="el-icon-s-shop"/>试驾数</div>
          <div style="padding:10px 0;text-align:center;font-weight:bold">{{ driveCount }}</div>
        </el-card>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <div id="main" style="width:500px; height:400px"></div>
      </el-col>
      <el-col :span="12">
        <div id="pie" style="width:500px; height:400px"></div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import * as echarts from 'echarts';
import {dashboard} from "@/api/car/main";


export default {
  name: "Home",
  data() {
    return {
      userCount: 0,
      goodsCount: 0,
      orderCount: 0,
      driveCount: 0,
      carTypeNameList: [],
      carTypeOrderCountValueList: [],
      carOrderWayCountList: [],
    }
  },
  created() {
    this.getDashboard();
  },

  methods: {
    getDashboard() {
      dashboard().then(response => {
        this.userCount = response.data.userCount;
        this.goodsCount = response.data.goodsCount;
        this.orderCount = response.data.orderCount;
        this.driveCount = response.data.driveCount;
        this.carTypeNameList = response.data.carTypeNameList;
        this.carTypeOrderCountValueList = response.data.carTypeOrderCountValueList;
        this.carOrderWayCountList = response.data.carOrderWayCountList;
        //进行图形渲染
        this.buildEcharts();
      })
    },
    buildEcharts() {       //使用mounted的目的是为了等页面元素渲染之后再触发
      var chartDom = document.getElementById('main');
      var myChart = echarts.init(chartDom);
      var charOption;
      charOption = {
        title: {
          text: '各种车型销量统计',
          subtext: '销量统计',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: this.carTypeNameList
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: this.carTypeOrderCountValueList,
            type: 'bar',
            label: {
              show: true,
              position: 'top', // 顶部显示
              formatter: '{c}', // 显示数据值
            },

          }
        ]
      };
      var pieDom = document.getElementById('pie');
      var pieChart = echarts.init(pieDom);
      var pieOption;

      pieOption = {
        title: {
          text: '贷款和全款比例',
          subtext: '比例图',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            type: 'pie',
            radius: '70%',
            label: {
              normal: {
                show: true,
                position: 'inner',
                textStyle: {
                  fontWeight: 300,
                  fontSize: 16,
                  color: "#fff"
                },
                formatter: '{d}%' //自定义显示格式(b:name, c:value, d:百分比)
              }
            },
            data: this.carOrderWayCountList,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      };
      // this.getDashboard();
      myChart.setOption(charOption);
      pieChart.setOption(pieOption);

    }
  },


}
</script>

<style scoped>

</style>
